// pages/order/order.ts
import {orderNum,orderNums,stopOrder,orderStatus} from '../../api/order'
Page({

    /**
     * 页面的初始数据
     */
    data: {
      tabsName:'1' as String,
      userID:-1 as number,
      comID:-1 as number,
      identity:'' as string,
      day:'' as string,
      teday:'' as string,
      history:'' as string,
      IngNum:'' as string,
      NoCount:'' as string,
      successDay:'' as string,
      successTeday:'' as string,
      successHistory:'' as string,
      successCount:'' as string,
      buttonColor:'info' as string,
      buttonText:'接'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },
    handleTabs(e:any){
      this.setData({
        tabsName:e.detail.name
      })
    },
    onClickLeft(){
      wx.navigateBack({
        delta:1,
      })
    },
    getNum(){
      const comID = wx.getStorageSync("userInfo").data.comID;
      const userID = wx.getStorageSync("userInfo").data.userID;
      const idCard = wx.getStorageSync('userInfo').data.position;
      if(idCard=='管家'){
        orderNum(comID, userID).then((res:any)=>{
          let num = res.data.今日未完成+res.data.昨日未完成+res.data.历史未完成;
          let nums = res.data.今日已完成+res.data.昨日已完成+res.data.历史已完成
          this.setData({
            day:res.data.今日未完成,
            teday:res.data.昨日未完成,
            history:res.data.历史未完成,
            successDay:res.data.今日已完成,
            successTeday:res.data.昨日已完成,
            successHistory:res.data.历史已完成,
            IngNum:res.data.进行中,
            NoCount:num,
            successCount:nums
          })
          console.log(res.data.今日未完成+res.data.昨日未完成+res.data.历史未完成)
        })
      }else{
        orderNums(comID, userID).then((res:any)=>{
          let num = res.data.今日未完成+res.data.昨日未完成+res.data.历史未完成;
          let nums = res.data.今日已完成+res.data.昨日已完成+res.data.历史已完成
          this.setData({
            day:res.data.今日未完成,
            teday:res.data.昨日未完成,
            history:res.data.历史未完成,
            successDay:res.data.今日已完成,
            successTeday:res.data.昨日已完成,
            successHistory:res.data.历史已完成,
            IngNum:res.data.进行中,
            NoCount:num,
            successCount:nums
          })
          console.log(res.data.今日未完成+res.data.昨日未完成+res.data.历史未完成)
        })
      }

    },
    handleModel(){
      const color = this.data.buttonColor
      const comID = wx.getStorageSync("userInfo").data.comID;
      const userID = wx.getStorageSync("userInfo").data.userID;
      if(color==='info'){
        stopOrder(comID,userID,1).then((res:any)=>{
          if(res.data.code==200){
            wx.showToast({
              title:'开始接单',
              icon:'success'
            })
            this.setData({
              buttonColor:'danger',
              buttonText:'停'
            })
          }
        })

      }else{
        stopOrder(comID,userID,0).then((res:any)=>{
          if(res.data.code==200){
            wx.showToast({
              title:'停止接单',
              icon:'success'
            })
            this.setData({
              buttonColor:'info',
              buttonText:'接'
            })
          }
        })

      }
    },
    getStatus(){
      const comID = wx.getStorageSync("userInfo").data.comID;
      const userID = wx.getStorageSync("userInfo").data.userID;
      orderStatus(comID,userID).then((res:any)=>{
        console.log(res)
        if(res.data==1){
          this.setData({
            buttonColor:'danger',
            buttonText:'停'
          })
        }else{
          this.setData({
            buttonColor:'info',
            buttonText:'接'
          })
        }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        userID: wx.getStorageSync("userInfo").data.userID,
        identity:wx.getStorageSync("userInfo").data.position
      })
      this.getNum();
      this.getStatus();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})